package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.exception.LoginException;

import com.dongdong.pojo.TbUser;
import com.dongdong.service.UserService;
import com.dongdong.util.ShiroUtils;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;



@Controller
@RequestMapping("user")
public class UserController {
    @Reference(timeout = 3000)
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    @RequestMapping("checkPhone")
    @ResponseBody
    public boolean checkPhone(String username){
        return userService.selectByUnsername(username);
    }
    @RequestMapping("register")
    @ResponseBody
    public Result register(TbUser user){
        //从redis中判断 key
        ValueOperations ops = redisTemplate.opsForValue();
        String code = (String)ops.get(user.getPhone());
//        String code = (String) redisTemplate.boundHashOps(user.getPhone()).get("code");
//        String code = (String) redisTemplate.opsForValue().get(user.getPhone());
        //判断验证码和注册用户是否一致
        if (!code.equals(user.getCode())) {
            return Result.respone(ResultEmnu.ERROR).setData(code);
        }

        //注册加密撒盐
        //获取所撒盐的长度
        String salt = ShiroUtils.generateSalt(6);
        String md5 = ShiroUtils.encryptPassword("MD5",user.getPassword(),salt,3);
        //存储所加的盐
        user.setSalt(salt);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        user.setIsMobileCheck("1");
        user.setPassword(md5);
        int i = userService.inserterInfo(user);
        if (i>0){
            return Result.respone(ResultEmnu.SUCCESS).setData(i);
        }
        return Result.respone(ResultEmnu.ERROR);
    }
    @RequestMapping("login")
    public String loginInfo(TbUser user, Model model){
        if (user.getUsername() == null || "".equals(user.getUsername()) || user.getPassword() == null || "".equals(user.getPassword())){
            model.addAttribute("message","请输入正确的账号密码");
            return "login";
        }
        System.out.println(user);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try{
            subject.login(token);
        }catch (UnknownAccountException e1){
            //账号不存在
            model.addAttribute("message","账号不存在");
            return "login";
        }catch (IncorrectCredentialsException e2){
            //密码错误
            model.addAttribute("message","密码错误");
            return "login";
        }catch (LoginException loginException) {
            loginException.printStackTrace();
            //账号审核尚未通过
            model.addAttribute("message", "账号审核未通过");
            return "login";
        }
        return "redirect:/index";
    }
    @RequestMapping("sendMessage")
    @ResponseBody
    public Result sendMessage(String phoneNum){
        rocketMQTemplate.convertAndSend("phoneNum",phoneNum);
        return Result.respone(ResultEmnu.SUCCESS);
    }
}
